package com.fanhao.dllo.soundfm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.entity.RadioStationResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.utils.CountToOthers;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/14.
 */
public class RadioAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private RadioStationResult.ResultEntity resultEntity;
    private ImageLoader imageLoader;


    public RadioAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(this.context);
        imageLoader = VolleySingleton.getInstance()._getImageLoader();
    }

    // 获得radioResult数据的方法
    public void addRadioData(RadioStationResult.ResultEntity resultEntity) {
        this.resultEntity = resultEntity;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new RecomViewHolder(
                    inflater.inflate(R.layout.radiostation_adapter_item_recom, parent, false));

        }
        if (viewType == 1) {
            return new RankViewHolder(
                    inflater.inflate(R.layout.radiostation_adapter_item_ranking, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        // 推荐界面
        if (holder instanceof RecomViewHolder) {
            RecomViewHolder recomHolder = (RecomViewHolder) holder;
            if (resultEntity != null) {
                List<RadioStationResult.ResultEntity.RecommandRadioListEntity> recomRadioList =
                        resultEntity.getRecommandRadioList();
                if (recomRadioList != null && recomRadioList.size() > 0) {
                    recomHolder.firstRecomTv.setText(recomRadioList.get(0).getRname());
                    recomHolder.firstRecomIv.setImageUrl(
                            recomRadioList.get(0).getPicPath(), BaseFragment.getImageLoader());
                    recomHolder.secondRecomTv.setText(recomRadioList.get(1).getRname());
                    recomHolder.secondRecomIv.setImageUrl(
                            recomRadioList.get(1).getPicPath(), BaseFragment.getImageLoader());
                    recomHolder.thirdRecomTv.setText(recomRadioList.get(2).getRname());
                    recomHolder.thirdRecomIv.setImageUrl(
                            recomRadioList.get(2).getPicPath(), BaseFragment.getImageLoader());
                }
            }

        }
        // 排行榜界面
        if (holder instanceof RankViewHolder) {
            RankViewHolder rankHolder = (RankViewHolder) holder;
            if (resultEntity != null) {
                List<RadioStationResult.ResultEntity.TopRadioListEntity> topList = resultEntity.getTopRadioList();
                if (topList != null && topList.size() > 0) {
                    rankHolder.firstTitleTv.setText(topList.get(0).getRname());
                    rankHolder.firstDescripTv.setText("正在直播: " + topList.get(0).getProgramName());
                    rankHolder.firstCountTv.setText(CountToOthers.getCountToWan(topList.get(0).getRadioPlayCount()));
                    rankHolder.firstRankIv.setImageUrl(topList.get(0).getRadioCoverLarge(), BaseFragment.getImageLoader());

                    rankHolder.secondTitleTv.setText(topList.get(1).getRname());
                    rankHolder.secondDescripTv.setText("正在直播: " + topList.get(1).getProgramName());
                    rankHolder.secondCountTv.setText(CountToOthers.getCountToWan(topList.get(1).getRadioPlayCount()));
                    rankHolder.secondRankIv.setImageUrl(topList.get(1).getRadioCoverLarge(), BaseFragment.getImageLoader());

                    rankHolder.thirdTitleTv.setText(topList.get(2).getRname());
                    rankHolder.thirdDescripTv.setText("正在直播: " + topList.get(2).getProgramName());
                    rankHolder.thirdCountTv.setText(CountToOthers.getCountToWan(topList.get(2).getRadioPlayCount()));
                    rankHolder.thirdRankIv.setImageUrl(topList.get(2).getRadioCoverLarge(), BaseFragment.getImageLoader());
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return 0;
        if (position == 1) return 1;

        return position;
    }

    // 内部类: RecomViewHolder 推荐界面
    class RecomViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout linearFirstRecom, linearSecondRecom, linearThirdRecom;
        private NetworkImageView firstRecomIv, secondRecomIv, thirdRecomIv;
        private TextView firstRecomTv, secondRecomTv, thirdRecomTv;

        public RecomViewHolder(View itemView) {
            super(itemView);
            linearFirstRecom = (LinearLayout) itemView.findViewById(R.id.linearFirst_recom_radio);
            linearSecondRecom = (LinearLayout) itemView.findViewById(R.id.linearSecond_recom_radio);
            linearThirdRecom = (LinearLayout) itemView.findViewById(R.id.linearThird_recom_radio);
            firstRecomIv = (NetworkImageView) itemView.findViewById(R.id.ivFirst_recom_radio);
            secondRecomIv = (NetworkImageView) itemView.findViewById(R.id.ivSecond_recom_radio);
            thirdRecomIv = (NetworkImageView) itemView.findViewById(R.id.ivThird_recom_radio);
            firstRecomTv = (TextView) itemView.findViewById(R.id.tvFirst_SubTitle_recom_radio);
            secondRecomTv = (TextView) itemView.findViewById(R.id.tvSecond_SubTitle_recom_radio);
            thirdRecomTv = (TextView) itemView.findViewById(R.id.tvThird_SubTitle_recom_radio);

        }
    }

    // 内部类: RankViewHolder 排行界面
    class RankViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout relativeRank, relativeFirstRank, relativeSecondRank, relativeThirdRank;
        private NetworkImageView firstRankIv, secondRankIv, thirdRankIv;
        private TextView firstTitleTv, secondTitleTv, thirdTitleTv, firstDescripTv, secondDescripTv, thirdDescripTv,
                firstCountTv, secondCountTv, thirdCountTv;

        public RankViewHolder(View itemView) {
            super(itemView);
            relativeRank = (RelativeLayout) itemView.findViewById(R.id.relative_ranking_radio);
            relativeFirstRank = (RelativeLayout) itemView.findViewById(R.id.relativeFirst_ranking_radio);
            relativeSecondRank = (RelativeLayout) itemView.findViewById(R.id.relativeSecond_ranking_radio);
            relativeThirdRank = (RelativeLayout) itemView.findViewById(R.id.relativeThird_ranking_radio);
            firstRankIv = (NetworkImageView) itemView.findViewById(R.id.ivFirst_ranking_radio);
            secondRankIv = (NetworkImageView) itemView.findViewById(R.id.ivSecond_ranking_radio);
            thirdRankIv = (NetworkImageView) itemView.findViewById(R.id.ivThird_ranking_radio);
            firstTitleTv = (TextView) itemView.findViewById(R.id.tvFirst_subTitle_ranking_radio);
            secondTitleTv = (TextView) itemView.findViewById(R.id.tvSecond_subTitle_ranking_radio);
            thirdTitleTv = (TextView) itemView.findViewById(R.id.tvThird_subTitle_ranking_radio);
            firstDescripTv = (TextView) itemView.findViewById(R.id.tvFirst_descrip_ranking_radio);
            secondDescripTv = (TextView) itemView.findViewById(R.id.tvSecond_descrip_ranking_radio);
            thirdDescripTv = (TextView) itemView.findViewById(R.id.tvThird_descrip_ranking_radio);
            firstCountTv = (TextView) itemView.findViewById(R.id.tvFirst_playCounts_ranking_radio);
            secondCountTv = (TextView) itemView.findViewById(R.id.tvSecond_playCounts_ranking_radio);
            thirdCountTv = (TextView) itemView.findViewById(R.id.tvThird_playCounts_ranking_radio);
        }
    }
}
